package com.enzoism.springboot.dive.bis.clone;

import java.util.Date;

/**
 * 1）对象的拷贝：JavaClone javaCopy = javaClone（直接将对象指针指向新对象）
 * 2）对象的浅克隆、深克隆（clone）
 * 2-1）数值类型（Integer等数字包装类型）基础类型（Java的8中基础类型）String类型（String不是基础类型）-> 这三种不收影响
 * 2-2）对象类型，浅拷贝时还是引用同一份对象（引用的对象地址相同）
 */
public class JavaClone implements Cloneable{
    private String userName;
    private Integer countNum;
    private int age;
    private Date date;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Integer getCountNum() {
        return countNum;
    }

    public void setCountNum(Integer countNum) {
        this.countNum = countNum;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public JavaClone(String userName, Integer countNum, int age, Date date) {
        this.userName = userName;
        this.countNum = countNum;
        this.age = age;
        this.date = date;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    // 自定义方法
    protected JavaClone myClone() throws CloneNotSupportedException {
        JavaClone clone = (JavaClone) super.clone();
        clone.setDate((Date) this.getDate().clone());
        return clone;
    }

}

